package com.jic.finance.base;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.ValueFilter;
import org.apache.logging.log4j.util.Strings;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by ZW on 2020/1/9 11:22
 */
public class JicBaseResult implements Serializable {

    private static final long serialVersionUID = -5007728971756456859L;

    /**
     * 成功
     */
    private static final String SUCCESS = "0000";

    /**
     * 通用失败(业务异常)
     */
    private static final String FAIL = "01";
    /**
     * 服务异常
     */
    public static final String SERVER_ERROR = "500";

    /**
     * 请求成功通用返回信息
     */
    private final static String ERR_OK_MSG = "执行成功";

    /**
     * 请求失败通用返回信息
     */
    private final static String ERR_FAIL_MSG = "执行失败";

    /**
     * 00 代表成功
     * 01 代表失败
     */
    private String errCode; //操作码

    private String errMsg; //操作原因

    private Object data; //附加数据

    private JicBaseResult(){}

    private JicBaseResult(String errCode){
        this.errCode = errCode;
    }

    /**
     * 成功
     * @return
     */
    public static JicBaseResult success(){
        JicBaseResult result = new JicBaseResult(SUCCESS);
        result.errMsg = ERR_OK_MSG;
        return result;
    }

    /**
     * 失败
     * @return
     */
    public static JicBaseResult fail(){
        JicBaseResult result = new JicBaseResult(FAIL);
        result.errMsg = ERR_FAIL_MSG;
        return result;
    }

    /**
     * 失败
     * @param errCode
     * @return
     */
    public static JicBaseResult fail(String errCode){
        JicBaseResult result = new JicBaseResult(errCode);
        result.errMsg = ERR_FAIL_MSG;
        return result;
    }


    /**
     * 设置返回结果
     * @param errMsg
     * @return
     */
    public JicBaseResult msg(String errMsg){
        this.errMsg = errMsg;
        return this;
    }


    /**
     * 设置返回数据
     * @return
     */
    public JicBaseResult data(Object data){
        this.data = data;
        return this;
    }

    /**
     * 构建返回值
     * @return
     */
    public String build(){
        if (StringUtils.isEmpty(this.errCode)){
            this.errCode = FAIL;
            this.errMsg = ERR_FAIL_MSG;
        }
        //处理溢出
        return JSONObject.toJSONString(this, (ValueFilter) (o, s, o1) -> {
            if (o1 instanceof Long){
                if ((Long) o1 >9007199254740992L){
                    return o1.toString();
                }
            }
            //处理日期格式
            if(o1 instanceof Date){
                return JicDateHelper.dateFormatter((Date)o1,JicDateHelper.DATETIME_FORMAT);
            }
            // 处理二层List
            if (o1 instanceof List) {
                return ((List) o1).stream().map(val->{
                    if (val instanceof Long){
                        if ((Long) val >9007199254740992L){
                            return val.toString();
                        }
                    }
                    return val;
                }).collect(Collectors.toList());
            }
            return o1;
        });
    }

    public String getErrCode() {
        return errCode;
    }

    public String getErrMsg() {
        return errMsg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }


}
